package com.farsight.leo.fssmartlock.activities;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.farsight.leo.fssmartlock.R;
import com.farsight.leo.fssmartlock.adapters.DeviceListAdapter;
import com.farsight.leo.fssmartlock.bases.BaseActivity;
import com.farsight.leo.fssmartlock.bases.BaseApplication;
import com.farsight.leo.fssmartlock.db.AuthCodeDao;
import com.farsight.leo.fssmartlock.db.AuthCodeEntity;
import com.farsight.leo.fssmartlock.db.DeviceEntity;
import com.farsight.leo.fssmartlock.net.OkHttpHelper;
import com.farsight.leo.fssmartlock.net.bases.BaseOkHttpCallback;
import com.farsight.leo.fssmartlock.net.bean.AuthCodeData;
import com.farsight.leo.fssmartlock.net.bean.DeviceData;
import com.farsight.leo.fssmartlock.net.bean.UserData;
import com.farsight.leo.fssmartlock.utils.LogUtils;
import com.farsight.leo.fssmartlock.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

public class ActivityDeviceList extends BaseActivity {

    private ListView mListView;
    private DeviceListAdapter mAdapter;
    private ArrayList<DeviceData> mList = new ArrayList<>();
    private UserData mUserData;
    private ProgressDialog progressDialog;

    public static void start(Context context) {
        Intent starter = new Intent(context, ActivityDeviceList.class);
        context.startActivity(starter);
    }

    @Override
    protected int set_layout_id() {
        return R.layout.activity_device_list;
    }

    @Override
    protected void initData() {
        mAdapter = new DeviceListAdapter(this, mList);
        mUserData = BaseApplication.getUserData();
    }

    @Override
    protected void initView() {
        mListView = findViewById(R.id.activity_device_list_list_view);
        mListView.setAdapter(mAdapter);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        LogUtils.log("begin to load device list.");
        updateList();
    }

    private void updateList(){
        OkHttpHelper.getInstance().getDeviceList(BaseApplication.getUserData(),
                new BaseOkHttpCallback<List<DeviceData>>() {
                    @Override
                    public void onError(String err) {
                        ToastUtils.showToast(err);
                    }

                    @Override
                    public void onSuccess(List<DeviceData> result) {
                        LogUtils.log(result.toString());
                        mList.clear();
                        mList.addAll(result);
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    protected void initListeners() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showProgressDialog();
                DeviceData data = mList.get(position);
                DeviceEntity entity = new DeviceEntity();
                entity.importData(data, mUserData.getUser_name());
                LogUtils.log(entity.toString());
                long i = BaseApplication.getDeviceDb().getDeviceDao().insertDevice(entity);
                LogUtils.log("insert result:"+i);
                if(i>0){
                    updateAuthCodes(data);
                }
            }
        });

    }

    private void showProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.down_loading_data));
        progressDialog.setCancelable(false);
        progressDialog.create();
        progressDialog.show();
    }

    private void dismissProgressDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    private void updateAuthCodes(DeviceData data) {
        OkHttpHelper.getInstance().getAuthCodeList(data, new BaseOkHttpCallback<List<AuthCodeData>>() {
            @Override
            public void onError(String err) {
                dismissProgressDialog();
                ToastUtils.showToast(err);
            }

            @Override
            public void onSuccess(List<AuthCodeData> result) {
                AuthCodeDao dao = BaseApplication.getDeviceDb().getAuthCodeDao();
                for (AuthCodeData code : result) {
                    AuthCodeEntity entity = new AuthCodeEntity();
                    entity.importData(code, mUserData.getUser_id());
                    dao.insert(entity);
                }
                dismissProgressDialog();
                ToastUtils.showToast(getString(R.string.data_updated_success));

            }
        });
    }

    public void refresh(View view) {
        updateList();
    }
}
